package Exec02;

import Exec01.Account;
import dao.BaseDao;

import java.sql.Connection;
import java.util.Date;
import java.util.List;

/**
 * @Author: yg
 * @create 2025-01-27 - 19:45
 * @
 */
public class AccountDAOImpl extends BaseDao implements AccountDAO {
    @Override
    public void insert(Connection conn, Account acct) {
        String sql = "insert into account(username, balance) values(?, ?)";
        update(conn, sql, acct.getUsername(), acct.getBalance());
    }

    @Override
    public void deleteById(Connection conn, int id) {
        String sql = "delete from account where id = ?";
        update(conn, sql, id);
    }

    @Override
    public void update(Connection conn, Account acct) {
        String sql = "update account set username = ?, balance = ? where id = ?";
        update(conn, sql, acct.getUsername(), acct.getBalance(), acct.getId());
    }

    @Override
    public Account getAccountById(Connection conn, int id) {
        String sql = "select id, username, balance from account where id = ?";
        Account account = getInstance(conn, Account.class, sql, id);
        return account;
    }

    @Override
    public List<Account> getAll(Connection conn) {
        String sql = "select id, username, balance from account";
        List<Account> list = getForList(conn, Account.class, sql);
        return list;
    }

    @Override
    public Long getCount(Connection conn) {

        String sql = "select count(*) from account";
        return getValue(conn, sql);
    }

    @Override
    public Date getMaxBirth(Connection conn) {
        return null;
    }
}
